# @File    : log_util.py
# @Project : Airtest_JBL
# 日志类
import logging
import os
import time
import colorlog
from logging.handlers import TimedRotatingFileHandler

log_colors_config = {
    "DEBUG": "blue",
    "INFO": "green",
    "WARNING": "red",
    "ERROR": "yellow",
    "CRITICAL": "red,bold",
}


class Logging:

    log_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + "\\" + 'logs'
    logger_name = 'OTA-LOG-' + ''.join(time.strftime('%Y-%m-%d')) + ''.join('.log')

    def create_log(self, logger_name=logger_name):
        self.logger = logging.getLogger(logger_name)
        self.logger.setLevel(logging.DEBUG)

        if not self.logger.handlers:
            """1、创建文件日志路径"""
            self.file_log_path = self.log_path + "\\" + self.logger_name
            self.file_handler = TimedRotatingFileHandler(filename=self.file_log_path, when="D", interval=1,
                                                         backupCount=5, delay=True, encoding='utf-8')
            self.file_handler.formatter = logging.Formatter(
                '[%(asctime)s]---[%(levelname)s]---[line:%(lineno)d]---%(message)s')
            self.logger.addHandler(self.file_handler)

            self.console_hander = colorlog.StreamHandler()
            self.console_hander.formatter = colorlog.ColoredFormatter(
                '%(log_color)s[%(asctime)s]---[%(levelname)s]---[line:%(lineno)d]---%(message)s',
                log_colors=log_colors_config)
            self.logger.addHandler(self.console_hander)

        return self.logger


def info(msg):
    Logging().create_log().info(msg)


def debug(msg):
    Logging().create_log().debug(msg)


def warning(msg):
    Logging().create_log().warning(msg)